define(function(require) {

const LINE_SELECT_OFFSET = 5;
const bezier = require('./bezier')
const { QBTEquB24ac, QBTEquSolution, quadraticBezier } = bezier
/**
 * 曲线的绘制条件其实更复杂，这里先简单处理了
 * 二维bezier曲线 xbb
 * @param ctx
 * @param cp 控制点
 * @param p1 终点
 * @param scale 缩放
 */
function drawCurve(ctx, cp, p1, scale) {
  scale = scale || 1
  ctx.quadraticCurveTo(cp[0] * scale, cp[1] * scale, p1[0] * scale, p1[1] * scale);
}

function didClickLine(points, mouseX, mouseY, scale) {
  const mouseXX = mouseX / scale;
  const mouseYY = mouseY / scale;
  const p = points;

  const xB24ac = QBTEquB24ac(p[0][0], p[1][0], p[2][0], mouseXX);
  const yB24ac = QBTEquB24ac(p[0][1], p[1][1], p[2][1], mouseYY);
  if (xB24ac > 0) {
    const xts = QBTEquSolution(p[0][0], p[1][0], p[2][0], mouseXX, xB24ac);
    for (let i = 0; i < xts.length; i++) {
      const t = xts[i];
      const y = quadraticBezier(p[0][1], p[1][1], p[2][1], t);
      console.log(y, mouseYY)
      if (abs(y - mouseYY) <= LINE_SELECT_OFFSET * scale) {
        return true;
      }
    }
  }

  if (yB24ac > 0) {
    const yts = QBTEquSolution(p[0][1], p[1][1], p[2][1], mouseYY, yB24ac);
    for (let j = 0; j < yts.length; j++) {
      const t = yts[j];
      const x = quadraticBezier(p[0][0], p[1][0], p[2][0], t);
      console.log(x, mouseXX)
      if (abs(x - mouseXX) <= LINE_SELECT_OFFSET * scale) {
        return true;
      }
    }
  }
}

return { drawCurve, didClickLine }
})